package com.wu.store.config;

import com.wu.store.dao.UserDao;
import com.wu.store.entity.User;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @Author: lifuzhao
 * @Date: 2020-02-15 22:23
 * @Project store
 */
public class OAuth2Realm extends AuthorizingRealm {
	@Autowired
	private UserDao userDao;

	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
		return null;
	}

	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
		UsernamePasswordToken usernamePasswordToken = (UsernamePasswordToken) token;
		String username = usernamePasswordToken.getUsername();
		User user = userDao.getUserByUserName(username);
		if (user == null) {
			throw new UnknownAccountException("该账号不存在");
		}
		if ("local".equals(user.getSource())) {
			return null;
		}
		return new SimpleAuthenticationInfo(user, user.getPassword(), "oAuth2Realm");
	}
}
